package com.example.yiqinmanagersystem.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yiqinmanagersystem.entity.HealthClock;
import com.example.yiqinmanagersystem.entity.NocvData;
import com.example.yiqinmanagersystem.mapper.HealthClockMapper;
import com.example.yiqinmanagersystem.service.HealthClockService;
import com.example.yiqinmanagersystem.vo.HealthClockVo;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 **@Author:youpeng
 **Created on 2022/9/12
 */
@RestController
@RequestMapping("/api")
public class HealthClockController {

    @Resource
    private HealthClockService healthClockService;
    /*
    *  @Description 获取健康打卡数据
    * */
    @GetMapping("/healthClock/listHealthClock")
    public ResponseResult listHealthClockData(HealthClockVo healthClockVo){
        //1.创建分页对象，并限制大小
        IPage<HealthClock> page = new Page<>(healthClockVo.getPage(), healthClockVo.getLimit());
        //2.创建模糊查询条件
        QueryWrapper<HealthClock> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(healthClockVo.getUsername()==null),"username",healthClockVo.getUsername());
        queryWrapper.like(!(healthClockVo.getPhone()==null),"phone",healthClockVo.getPhone());
        //3.查询数据库
        IPage<HealthClock> dataIPage = healthClockService.page(page, queryWrapper);
        //4.返回json数据格式封装
        if(dataIPage.getSize()!=0){
            return new ResponseResult(ResponseCode.SUCCESS, Math.toIntExact(page.getTotal()),page.getRecords());
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * @Description 添加或修改健康打卡数据
    * */
    @PostMapping("/healthClock/addHealthClock")
    public ResponseResult addHealthClock(HealthClock healthClock){
        boolean b = healthClockService.saveOrUpdate(healthClock);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * @Description 删除健康打卡数据逻辑
    * @Params Integer id
    * @Return
    * */
    @PostMapping("/healthClock/deleteHealthClockById")
    public ResponseResult deleteHealthClockById(Integer id){
        boolean b = healthClockService.removeById(id);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }
}
